package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import JurisPro.Sistema;
import JurisPro.modelo.Agenda;
import JurisPro.modelo.FilaEventos;
import JurisPro.modelo.Usuario;

/**
 * Servlet implementation class AutenticacaoServlet
 */
public class AutenticacaoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Sistema sistema = Sistema.getInstance();   
	 /**
     * @see HttpServlet#HttpServlet()
     */
    public AutenticacaoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
   
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pagina = request.getParameter("pagina");
		
		if(pagina.equalsIgnoreCase("logar")){
			String login = request.getParameter("login");
			String senha = request.getParameter("senha");
			
			if(sistema.autenticarUsuario(login, senha)){
				 Usuario usuario = sistema.usuarioLogado(login, senha);
				 HttpSession session = request.getSession();
				 session.setAttribute("user", usuario);
				 request.setAttribute("titulo", "Jurispro");
				 if(usuario.getTipo()==1){
					 request.setAttribute("filaespera", sistema.processosParaReceber(usuario.getId()));
					 RequestDispatcher dispache = request.getRequestDispatcher("pages/inicio-adm.jsp");
					 dispache.forward(request, response);
					 return;
				 }
				 if(usuario.getTipo()==2){
					 request.setAttribute("filaespera", sistema.processosParaReceber(usuario.getId()));
					 request.setAttribute("agendahoje", sistema.listarAgendaDeHoje(usuario.getId()));
					 RequestDispatcher dispache = request.getRequestDispatcher("pages/index.jsp");
					 dispache.forward(request, response);
					 return;
				 }
				 if(usuario.getTipo()==3){
					 
					 request.setAttribute("infoUsuario", new Usuario());
					 request.setAttribute("idUsarioAgenda", 0);
					 request.setAttribute("destino", sistema.listarUsuarios());
					 request.setAttribute("agenda", new ArrayList<Agenda>());						 
					 request.setAttribute("eventos",new ArrayList<FilaEventos>());
					 RequestDispatcher dispache = request.getRequestDispatcher("pages/inicio-secretaria.jsp");
					 dispache.forward(request, response);
					 return; 
				 }	 	   
		   } // Usuario nao autenticado
			 RequestDispatcher dispache = request.getRequestDispatcher("login.jsp");
			 dispache.forward(request, response);
		}				
	}
	

}
